home *** CD-ROM | disk | FTP | other *** search
- #include <io.h>
- #include <math.h>
- #include <string.h>
- #include <stdio.h>
- #include <assert.h>
-
- void xpfsf();
-
- int main()
- {
- xpfsf();
- }
-
- #define PANSWER1 "<teststr><a><9876><6789><face><64206>"
- #define PANSWER2 "<twoth ><b ><9876 >< 06789>< 1234><face><FACE><face><64206>"
- #define PANSWER3 "< freef>< c>< -659876>< 126789><+1234>< 1234><-1234>< face4321>< FACE9876>< face4290><-1330>"
- #define PANSWER4 "<-4201><-12><253><-120368><1762><543213>"
- #define PANSWER5 "<1.234500><903.142><903.141600><1.200000e+215><1.200000E-139><2.71828e-006><7.8192E+005><0.000003><781922.301000>"
-
- // --- from xmath.c
- int roughly(double x, double y)
- {
- if(y==0.0)
- return (fabs(x) < 1.0e-6);
- else
- return (fabs((y-x)/y) < 1.0e-6);
- }
-
-
- void xpfsf()
- {
- FILE *fp, tempin, tempout;
- char buf[300], bufa[100], bufb[100], bufc[100], bufd[100];
- int n1,n2,n3,n4,n5,n6,n7;
- short s1,s2;
- unsigned long l1,l2,l3,l4,l5;
- char c1,c2;
- float f1,f2,f3,f4,f5,f6,f7,f8,f9;
-
- // --- test the printf/scanf family of functions
- printf("==> starting xpfsf <==\n");
-
- // --- first we test sprintf
- // --- sprintf/printf/fprintf all use one function for formatting, so we need only test sprintf
- assert(sprintf(buf,"<%s><%c><%d><%u><%x><%u>","teststr",'a',9876,6789,0xface,0xface)==strlen(PANSWER1));
- assert(strcmp(buf,PANSWER1)==0);
-
- assert(sprintf(buf,"<%-10.5s><%-3c><%-6.4d><%6.5u><%6.3u><%x><%X><%x><%u>",
- "twothree",'b',9876,6789,1234,0xface,0xface,0xface,0xface)==strlen(PANSWER2));
- assert(strcmp(buf,PANSWER2)==0);
-
- assert(sprintf(buf,"<%10.5s><%2c><%9.5ld><%+9.5lu><%+d><% d><%+d><%10lx><%10lX><%10lx><%d>",
- "freefour",'c',-659876L,126789L,1234,1234,-1234,0xface4321L,0xface9876L,0xface4290L,0xface)==strlen(PANSWER3));
- assert(strcmp(buf,PANSWER3)==0);
-
- assert(sprintf(buf,"<%i><%hi><%o><%li><%hd><%lo>",-4201,-12,0253,-120368L,1762,0543213L,0234561L)==strlen(PANSWER4));
- assert(strcmp(buf,PANSWER4)==0);
-
- assert(sprintf(buf,"<%f><%2.3f><%f><%e><%E><%g><%10.5G><%f><%f>",
- 1.2345,903.1416,903.1416,1.2e215,1.2e-139,0.00000271828,781922.301,0.00000271828,781922.301)==strlen(PANSWER5));
- assert(strcmp(buf,PANSWER5)==0);
- printf("passed sprintf tests... ");
-
- // --- now we test sscanf
- // --- sprintf/printf/fprintf all use one function for formatting, so we need only test sprintf
- memset(bufa,0,sizeof(bufa)); memset(bufb,0,sizeof(bufb)); memset(bufc,0,sizeof(bufc)); memset(bufd,0,sizeof(bufd)); n1=0;
- strcpy(buf,"hello world mars calling 4201");
- assert(sscanf(buf,"%s%s%s %s %d",bufa,bufb,&bufc[0],&bufd[0],&n1)==5);
- assert(strcmp(bufa,"hello")==0); assert(strcmp(bufb,"world")==0);
- assert(strcmp(bufc,"mars")==0); assert(strcmp(bufd,"calling")==0);
- assert(n1==4201);
-
- n1=n2=s1=c1=c2=0; l1=l2=0L;
- strcpy(buf,"yn-4201;0123face;0123456;cab2fade");
- assert(sscanf(buf,"%c%c%hd;%o%x;%lo;%lx",&c1,&c2,&s1,&n1,&n2,&l1,&l2)==7);
- assert(c1=='y'); assert(c2=='n');
- assert(s1==-4201); assert(n1==0123); assert(n2==0xface);
- assert(l1==42798L); assert(l1==0123456L); assert(l2==3400727262L); assert(l2==0xcab2fadeL);
-
- f1=f2=f3=0.0;
- strcpy(buf,"2.67e-283; 3.14; 2.718e59;");
- assert(sscanf(buf,"%f; %e; %g;",&f1,&f2,&f3)==3);
- assert(fabs(f1-(2.67e-283)) < 1e-200); assert(fabs(f2-3.14)<0.01); assert(fabs(f3-2.718e59) < 1e50);
-
- printf("passed sscanf tests...\n");
-
- // --- test fprintf
- fp = fopen("testdata.txt","w"); // --- open for write and truncate
-
- assert(fp!=NULL && fp>stdprn);
- assert(ferror(fp)==0 && feof(fp)==0);
-
- assert(fprintf(fp,"<%s><%c><%d><%u><%x><%u>\n","teststr",'a',9876,6789,0xface,0xface)==strlen(PANSWER1)+1);
- assert(fprintf(fp,"<%-10.5s><%-3c><%-6.4d><%6.5u><%6.3u><%x><%X><%x><%u>\n",
- "twothree",'b',9876,6789,1234,0xface,0xface,0xface,0xface)==strlen(PANSWER2)+1);
- assert(fprintf(fp,"<%10.5s><%2c><%9.5ld><%+9.5lu><%+d><% d><%+d><%10lx><%10lX><%10lx><%d>\n",
- "freefour",'c',-659876L,126789L,1234,1234,-1234,0xface4321L,0xface9876L,0xface4290L,0xface)==strlen(PANSWER3)+1);
- assert(fprintf(fp,"<%i><%hi><%o><%li><%hd><%lo>\n",-4201,-12,0253,-120368L,1762,0543213L,0234561L)==strlen(PANSWER4)+1);
- assert(fprintf(fp,"<%f><%2.3f><%f><%e><%E><%g><%10.5G><%f><%f>\n",
- 1.2345,903.1416,903.1416,1.2e215,1.2e-139,0.00000271828,781922.301,0.00000271828,781922.301)==strlen(PANSWER5)+1);
-
- assert(fclose(fp)==0);
-
- // --- now verify fprintf output, and test fscanf
- fp = fopen("testdata.txt","r"); // --- open for write and truncate
- assert(fp!=NULL && fp>stdprn);
- assert(ferror(fp)==0 && feof(fp)==0);
-
- // --- first line of testdate
- bufa[0]='\0'; c1='Y'; n1=n2=n3=n4=0;
- assert(fscanf(fp,"<%7s><%c><%d><%u><%x><%u> ",bufa,&c1,&n1,&n2,&n3,&n4)==6);
- assert(strcmp(bufa,"teststr")==0); assert(c1=='a');
- assert(n1==9876); assert(n2==6789); assert(n3==0xface); assert(n4==64206);
-
- // --- second line of testdate
- bufa[0]='\0'; c1='Y'; n1=n2=n3=n4=n5=n6=0; l1=0L;
- assert(fscanf(fp,"<%s ><%c ><%d><%u><%u><%x><%X><%x><%u> ",bufa,&c1,&n1,&n2,&n3,&n4,&l1,&n5,&n6)==9);
- assert(strcmp(bufa,"twoth")==0); assert(c1=='b');
- assert(n1==9876); assert(n2==6789); assert(n3==1234); assert(n4==0xface); assert(n5==0xface);
- assert(n6==64206); assert(l1==0xfaceL);
-
- // --- third line of testdate
- bufa[0]='\0'; c1='Y'; n1=n2=n3=n4=0; l1=l2=l3=l4=l5=0L;
- assert(fscanf(fp,"< %5s>< %c><%ld><%lu><%d>< %d><%d>< %lx>< %lX>< %lx><%d> ",bufa,&c1,&l1,&l2,&n1,&n2,&n3,&l3,&l4,&l5,&n4)==11);
-
- assert(strcmp(bufa,"freef")==0); assert(c1=='c');
- assert(n1==1234); assert(n2==1234); assert(n3==-1234); assert(n4==-1330);
- assert(l1==-659876L); assert(l2==126789L); assert(l3==0xface4321L); assert(l4==0xFACE9876L); assert(l5==0xface4290L);
-
- // --- fourth line of testdate
- n1=n2=0; s1=s2=0; l1=l2=0L;
- assert(fscanf(fp,"<%i><%hi><%o><%li><%hd><%lo> ",&n1,&s1,&n2,&l1,&s2,&l2)==6);
-
- assert(n1==-4201); assert(s1==-12); assert(n2==0253); assert(l1==-120368L);
- assert(s2==1762); assert(l2==0543213L);
-
- // --- fifth line of testdate
- f1=f2=f3=f4=f5=f6=f7=f8=f9=0.0;
- assert(fscanf(fp,"<%f><%e><%g><%f><%e><%g><%f><%e><%f> ",&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9)==9);
- assert(roughly(f1,1.2345)); assert(roughly(f2,903.142)); assert(roughly(f3,903.141600));
- assert(roughly(f4,1.2e215)); assert(roughly(f5,1.2e-139)); assert(roughly(f6,0.00000271828));
- assert(roughly(f7,7.8192E+005));assert(roughly(f8,0.000003)); assert(roughly(f9,781922.301));
-
- printf("passed fprintf/fscanf tests... ");
- assert(fclose(fp)==0);
-
-
- // --- test scanf/printf from/to stdin/stdout
- // --- test printf
-
- // --- note that assert() writes to stdout
- // --- so you should check the "testout.txt" file for assertion failure
- fp = fopen("testdata.txt","w"); // --- open for write and truncate
-
- assert(fp!=NULL && fp>stdprn);
- memcpy(&tempout,stdout,sizeof(FILE));
- memcpy(stdout,fp,sizeof(FILE));
-
- printf("%s %s %d\n","hello world","mars calling",4201);
- printf("%c%c%hd;%o%x;%lo;%lx\n",'y','n',-4201,0123,0xface,0123456L,0xcab2fadeL);
- printf("%g; %f; %g;\n",2.67e-283,3.14,2.718e59);
-
- assert(fclose(stdout)==0);
- memcpy(stdout,&tempout,sizeof(FILE)); // --- return stdout to previous settings
-
-
- // --- now we read some bytes from the file
- fp = fopen("testdata.txt","r");
- // --- now we reassign stdin and issue standard i/o calls
- memcpy(&tempin,stdin,sizeof(FILE));
- assert(fp!=NULL && fp>stdprn);
- memcpy(stdin,fp,sizeof(FILE));
-
- memset(bufa,0,sizeof(bufa)); memset(bufb,0,sizeof(bufb));
- memset(bufc,0,sizeof(bufc)); memset(bufd,0,sizeof(bufd)); n1=0;
-
- assert(scanf("%s%s%s %s %d",bufa,bufb,&bufc[0],&bufd[0],&n1)==5);
- assert(strcmp(bufa,"hello")==0); assert(strcmp(bufb,"world")==0);
- assert(strcmp(bufc,"mars")==0); assert(strcmp(bufd,"calling")==0);
- assert(n1==4201);
-
- n1=n2=s1=c1=c2=0; l1=l2=0L;
- assert(scanf("%c%c%hd;%o%x;%lo;%lx",&c1,&c2,&s1,&n1,&n2,&l1,&l2)==7);
- assert(c1=='y'); assert(c2=='n');
- assert(s1==-4201); assert(n1==0123); assert(n2==0xface);
- assert(l1==42798L); assert(l1==0123456L); assert(l2==3400727262L); assert(l2==0xcab2fadeL);
-
- f1=f2=f3=0.0;
- assert(scanf("%f;%e;%g;",&f1,&f2,&f3)==3);
- assert(roughly(f1,2.67e-283)); assert(roughly(f2,3.14)); assert(roughly(f3,2.718e59));
- assert(fclose(stdin)==0);
- memcpy(stdin,&tempin,sizeof(FILE));
- printf("passed printf/scanf tests...\n");
-
- printf("==> finished xpfsf <==\n");
- return;
- }
-